package com.umerclass.ui.user;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.umerclass.R;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.okhttp.OkSimpleRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.ui.anmo.CountDownTimer;
import com.umerclass.utils.Constants;
import butterknife.BindView;
import butterknife.OnClick;

public class NoticePhoneActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private int type = 1;//1修改通知手机 2修改绑定手机
    private boolean loginCode = false;
    private String phone1 = "";
    private String code1 = "";
    private boolean click = false;//是否可以点击获取验证码
    CountDownTimer timer1 = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvSendCode.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tvSendCode.setText("重新获取验证码");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer1.cancel();
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_notice_phone;
    }

    @Override
    public void init() {

        type = super.getIntent().getIntExtra("type",1);
        if(type == 1){
            tvTitle.setText("修改通知手机");
        }else if(type == 2){
            tvTitle.setText("修改绑定手机");
        }
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    phone1 = s.toString();
                    click = true;
                    tvSendCode.setBackgroundResource(R.drawable.shape_two_press);
                } else {
                    click = false;
                    phone1 = "";
                    tvSendCode.setBackgroundResource(R.drawable.shape_two_normal);
                }

                if (TextUtils.isEmpty(phone1) || TextUtils.isEmpty(code1)) {
                    loginCode = false;
                    btnOk.setBackgroundResource(R.drawable.shape_three_normal);
                } else {
                    btnOk.setBackgroundResource(R.drawable.shape_two_press);
                    loginCode = true;
                }
            }
        });

        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    code1 = s.toString();
                } else {
                    code1 = "";
                }

                if (TextUtils.isEmpty(phone1) || TextUtils.isEmpty(code1)) {
                    loginCode = false;
                    btnOk.setBackgroundResource(R.drawable.shape_three_normal);
                } else {
                    btnOk.setBackgroundResource(R.drawable.shape_two_press);
                    loginCode = true;
                }
            }
        });

    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back, R.id.btn_ok, R.id.tv_send_code})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.btn_ok:
                break;
            //获取验证码
            case R.id.tv_send_code:
                if (click) {
                    timer1.start();
                    OkSimpleRequest request = new OkSimpleRequest<>(R.id.authPhone,
                            Constants.authPhone);
                    request.addParams("phone", phone1);
                    requestOkhttpSimple(request);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

        switch (what) {
            case R.id.authPhone:
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.authPhone:
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }
}
